package com.crm.action;

import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version 1.0
 */
public abstract class JSONController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
        JSONObject json = handleAjaxRequest(request, response);
        response.setContentType("text/Xml;charset=GBK");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.println(json.toString());
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }

        return null;
    }

    protected abstract JSONObject handleAjaxRequest(HttpServletRequest request,
                                HttpServletResponse response) throws Exception;
}
